package com.jnu.happyrunningwithmusic;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class MusicService extends Service {
    private List<String> slow_paths=new ArrayList<>();
    private List<String> middle_paths=new ArrayList<>();
    private List<String> fast_paths=new ArrayList<>();
    private String path="https://cdn.jsdelivr.net/gh/hhh-yao/hhh-yao.github.io@3.0/抒情/The sound of Silence.mp3";
    private String origin_path;
    private MediaPlayer mediaPlayer;


    public MusicService() {
        slow_paths.add("https://cdn.jsdelivr.net/gh/hhh-yao/hhh-yao.github.io@3.0/抒情/夜空中最亮的星.mp3");
        slow_paths.add("https://cdn.jsdelivr.net/gh/hhh-yao/hhh-yao.github.io@3.0/抒情/five hundreds miles.mp3");
        slow_paths.add("https://cdn.jsdelivr.net/gh/hhh-yao/hhh-yao.github.io@3.0/抒情/The sound of Silence.mp3");
        middle_paths.add("https://cdn.jsdelivr.net/gh/hhh-yao/hhh-yao.github.io@3.0/轻快/I'm yours.mp3");
        middle_paths.add("https://cdn.jsdelivr.net/gh/hhh-yao/hhh-yao.github.io@3.0/轻快/rather be.mp3");
        middle_paths.add("https://cdn.jsdelivr.net/gh/hhh-yao/hhh-yao.github.io@3.0/轻快/viva la vida.mp3");
        fast_paths.add("https://cdn.jsdelivr.net/gh/hhh-yao/hhh-yao.github.io@3.0/快歌/bangbang.mp3");
        fast_paths.add("https://cdn.jsdelivr.net/gh/hhh-yao/hhh-yao.github.io@3.0/快歌/purple passion.mp3");
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        //throw new UnsupportedOperationException("Not yet implemented");
        return new MyBinder();
    }

    public class MyBinder extends Binder{
        public  void playPause(){
            if(mediaPlayer.isPlaying()){
                mediaPlayer.pause();
            }
            else {
                mediaPlayer.start();
            }
        }
        public boolean isPlaying(){
            return mediaPlayer.isPlaying();
        }
        public int getDuration(){
            return mediaPlayer.getDuration();
        }
        public int getCurrentPosition(){
            return mediaPlayer.getCurrentPosition();
        }
        public void seekTo(int msec){
            mediaPlayer.seekTo(msec);
        }
        public void reset(double average){
            mediaPlayer.stop();
            mediaPlayer.reset();
            Random random=new Random();
            int r;
            try {
                if(average>9){
                    origin_path=path;

                    r=random.nextInt(fast_paths.size());
                    path=fast_paths.get(r);
                    while (origin_path==path){
                        origin_path=path;
                        r=random.nextInt(fast_paths.size());
                        path=fast_paths.get(r);
                    }
                }
                else if(average>6){
                    origin_path=path;

                    r=random.nextInt(middle_paths.size());
                    path=middle_paths.get(r);
                    while (origin_path==path){
                        origin_path=path;
                        r=random.nextInt(middle_paths.size());
                        path=middle_paths.get(r);
                    }
                }
                else{
                    origin_path=path;

                    r=random.nextInt(slow_paths.size());
                    path=slow_paths.get(r);
                    while (origin_path==path){
                        origin_path=path;
                        r=random.nextInt(slow_paths.size());
                        path=slow_paths.get(r);
                    }
                }
                mediaPlayer.setDataSource(path);
                mediaPlayer.prepare();
                mediaPlayer.seekTo(0);
                mediaPlayer.start();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    @Override
    public void onCreate() {
        super.onCreate();
        mediaPlayer=new MediaPlayer();
        try {
            mediaPlayer.setDataSource(path);
            mediaPlayer.prepare();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}